/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var script = document.createElement('SCRIPT');

script.src = "https://api.vk.com/method/database.getCountries?need_all=1&v=5.16&count=1000&callback=callbackFunc";

document.getElementsByTagName("head")[0].appendChild(script);

function callbackFunc(result) {
    //  alert(result.response.count);
    var aaa = result.response.items;

    var sel = document.createElement('select');
    $(sel).attr({'id' : 'form_region'});
    $(sel).attr({'data-placeholder' : 'Выбрать страну'});
    $(sel).attr({'tabindex' : '4'});
    $(sel).attr({'name' : 'country'});
    $(sel).css({'width' : '300px'});
    $(sel).addClass('chosen-select_project');

    var opt = document.createElement('option');
    $(opt).val(0);
    $(opt).text('Выберите страну');
    $(sel).append(opt);

    for (var key in aaa) {
        if (aaa.hasOwnProperty(key)) {
            var opt = document.createElement('option');
            $(opt).val(aaa[key].id);
            $(opt).text(aaa[key].title);
            $(sel).append(opt);
        }
    }
    $(sel).change(function() {
        city($(this).val());
    });
    $('#countrydiv').append(sel)
}

function city(id) {
	// Setup maxHeight for IE6
	$.autoComplete.defaults.maxHeight = 250;
	// Normal Auto-complete initiation
	 $('#citydiv').html(' <input type="text" id="form_city"  name="city" value="" />');
	// Auto-complete using separate ajax script/post values
	$('#form_city').autoComplete({
		ajax: '/utility/getcity',
		postData: {
			countr: $('#form_region').val()
		},
		postFormat: function(event, ui){
			// Add the current timestamp to each request
			ui.data.requestTimestamp = (new Date()).getTime();

            // Return the data object to be passed with the ajax function
            return ui.data;
        }
    });


}
